模块:Util
Utility stuff local xtable = require('Module:Table') local util = {} -- ---------------------------------------------------------------------------- -- util.cast -- ---------------------------------------------------------------------------- util.cast = {} util.cast.bool_false = {'false', '0', 'disabled', 'off', 'no', ''} function util.cast.boolean(value) -- Takes an abitary value and casts it to a bool value -- -- for strings false will be according to util.cast.bool_false local t = type(value) if t 'nil' then return false elseif t 'boolean' then return value elseif t 'number' then if value 0 then return false end return true elseif t 'string' then local tmp = string.lower(value) for _, v in ipairs(util.cast.bool_false) do if v tmp then return false end end return true else error(string.format('value "%s" of type "%s" is not a boolean', value, t)) end end function util.cast.number(value, args) -- Takes an abitary value and attempts to cast it to int -- -- args: -- default: for strings, if default is nil and the conversion fails, an error will be returned -- min: error if max if args nil then args = {} end local t = type(value) local val if t 'nil' then val = nil elseif t 'boolean' then if value then val = 1 else val = 0 end elseif t 'number' then val = value elseif t 'string' then val = tonumber(value) end if val nil then if args.default ~= nil then val = args.default else error(string.format('value "%s" of type "%s" is not an integer', tostring(value), t)) end end if args.min ~= nil and val < args.min then error(string.format('"%i" is too small. Minimum: "%i"', val, args.min)) end if args.max ~= nil and val > args.max then error(string.format('"%i" is too large. Maximum: "%i"', val, args.max)) end return val end -- ---------------------------------------------------------------------------- -- util.html -- ---------------------------------------------------------------------------- util.html = {} function util.html.abbr(abbr, text, class) tag = mw.html.create('abbr') tag :attr('title', text or '') :attr('class', class or '') :wikitext(abbr or ' ') :done() return tostring(tag) end function util.html.error(args) -- Create an error message box -- -- Args: -- msg - message if args nil then args = {} end local err = mw.html.create('span') err :attr('class', 'module-error') :wikitext('Module Error: ' .. (args.msg or '')) :done() return tostring(err) end -- ---------------------------------------------------------------------------- -- util.misc -- ---------------------------------------------------------------------------- util.misc = {} function util.misc.is_frame(frame) -- the type of the frame is a table containing the functions, so check whether some of these exist -- should be enough to avoid collisions. return not(frame nil or type(frame) ~= 'table' or (frame.argumentPairs nil and frame.callParserFunction nil)) end function util.misc.get_frame(frame) if util.misc.is_frame(frame) then return frame end return mw.getCurrentFrame() end function util.misc.add_category(categories, args) -- categories: table of categories -- args: table of extra arguments -- namespace: id of namespace to validate against -- ingore_blacklist: set to non-nil to ingore the blacklist if type(categories) 'string' then categories = {categories} end if args nil then args = {} end local title = mw.title.getCurrentTitle() local sub_blacklist = { doc = true, sandbox = true, sandbox2 = true, testcases = true, } if args.namespace ~= nil and title.namespace ~= args.namespace then return '' end if args.ingore_blacklist nil and sub_blacklisttitle.subpageText then return '' end local cats = {} for i, cat in ipairs(categories) do catsi = string.format('Category:%s', cat) end return table.concat(cats) end -- ---------------------------------------------------------------------------- -- util.smw -- ---------------------------------------------------------------------------- util.smw = {} util.smw.data = {} util.smw.data.rejected_namespaces = xtable:new({'User'}) function util.smw.query(query, frame) -- Executes a semantic media wiki #ask query and returns the result as an -- array containing each row as table. -- -- query: table of query arguments to pass -- frame: current frame object -- the characters here for sep/header/propsep are control characters; I'm farily certain they should not appear in regular text. query.sep = '�' query.propsep = '' query.headersep = '' query.format = 'array' query.headers = 'plain' local result = frame:callParserFunction('#ask:', query) -- " Some subquery has no valid condition." if mw.ustring.find(result, 'data%-title="Error"') ~= nil then error(mw.ustring.sub(result, mw.ustring.find(result, ' 6 -- find_nested_array(arg=10, tbl=data, key='a'} -> nil -- -> returns "6" -- -- args: Table containing: -- value: value of the argument -- tbl: table of valid options -- key: key or table of key of in tbl -- rtrkey: if key is table, return this key instead of the value instead -- rtrvalue: default: true local rtr if type(args.key) 'table' then for _, item in ipairs(args.tbl) do for _, k in ipairs(args.key) do if itemk args.value then rtr = item break end end end elseif args.key nil then for _, item in ipairs(args.tbl) do if item args.value then rtr = item break end end else for _, item in ipairs(args.tbl) do if itemargs.key args.value then rtr = item break end end end if rtr nil then return rtr end if args.rtrkey ~= nil then return rtrargs.rtrkey elseif args.rtrvalue or args.rtrvalue nil then return args.value else return rtr end end -- ---------------------------------------------------------------------------- return util